﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace KCL.Code.Data
{
    public class MaterialExportDAO : BaseDAO
    {
        public MaterialExport GetMaterialExportByID(int id)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var materialExport = (from mexp in ctx.MaterialExports
                                      where mexp.id == id
                                      select mexp).FirstOrDefault();
                return materialExport;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<MaterialExport> GetAllMaterialExports()
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var lstMaterialExport = from mexp in ctx.MaterialExports
                                        orderby mexp.date descending
                                        select mexp;
                return lstMaterialExport.AsEnumerable().ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool Insert(MaterialExport me)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                ctx.MaterialExports.InsertOnSubmit(me);
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(MaterialExport me)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                var materialExportToUpdate = (from mexp in ctx.MaterialExports
                                              where mexp.id == me.id
                                              select mexp).FirstOrDefault();
                materialExportToUpdate.materialCode = me.materialCode;
                materialExportToUpdate.quantity = me.quantity;
                materialExportToUpdate.receivePlace = me.receivePlace;
                materialExportToUpdate.receiver = me.receiver;
                materialExportToUpdate.exporter = me.exporter;
                materialExportToUpdate.date = me.date;
                materialExportToUpdate.note = me.note;
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(MaterialExport me)
        {
            try
            {
                KCLDataContext ctx = new KCLDataContext();
                ctx.MaterialExports.DeleteOnSubmit(ctx.MaterialExports.Where(
                    mexp => mexp.id == me.id).FirstOrDefault());
                ctx.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public int GetMaxID()
        {
            KCLDataContext ctx = new KCLDataContext();
            int? maxid = (from mexp in ctx.MaterialExports
                          select (int?)mexp.id).Max();
            return maxid == null ? -1 : (int)maxid;
        }
    }
}